﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using DLTN.Data;
using DLTN.Entities;
using System.Data;

public partial class Tour_ListTour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Params["IdSubTour"] != null)
            {
                Pager1.CurrentIndex = 1;
                DisplayTour(Pager1.CurrentIndex);
                pnchuyenmuc.Visible = false;
                pnTour.Visible = true;
                ltrTitle.Text = GetSubTour(Request.Params["IdSubTour"]) + " - ";
            }
            else if (Request.Params["TuKhoa"] != null)
            {
                Pager1.CurrentIndex = 1;
                DisplayTour(Pager1.CurrentIndex);
                pnchuyenmuc.Visible = false;
                pnTour.Visible = true;
                ltrTitle.Text = GetTieuDe(Request.Params["TuKhoa"]) + " - ";
            }
            else if (Request.Params["TuKhoaCM"] != null)
            {
                Pager2.CurrentIndex = 1;
                HienThiChuyenMuc(Pager2.CurrentIndex);
                pnchuyenmuc.Visible = true;
                pnTour.Visible = false;
                ltrTitle.Text = GetTieuDe(Request.Params["TuKhoaCM"]) + " - ";
            }
            else
            {
                Pager1.CurrentIndex = 1;
                DisplayTour(Pager1.CurrentIndex);
                pnchuyenmuc.Visible = false;
                pnTour.Visible = true;
                ltrTitle.Text = "";
            }
        }
    }

    private void HienThiChuyenMuc(int PageNo)
    {
        int TotalRows = 0;
        string whereClause = "";
        whereClause += " dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + Request.Params["TuKhoaCM"].ToString() + "%' Or dbo.fuChuyenCoDauThanhKhongDau(TomTat) like N'%" + Request.Params["TuKhoaCM"].ToString() + "%'";
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("ChuyenMuc_GetPaged", whereClause, "NgayCapNhat DESC", PageNo - 1, Pager2.PageSize);
        dlBaiViet.DataSource = dsTinTuc;
        dlBaiViet.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager2.ItemCount = TotalRows;
        if (TotalRows <= Pager2.PageSize)
            Pager2.Visible = false;
        else
            Pager2.Visible = true;
        return;
    }

    // Hiển thị Tour
    private void DisplayTour(int PageNo)
    {
        int TotalRows = 0;
        string whereClause = "";
        //string strKey = Request.Params["TuKhoa"];

        //// Trường hợp khi gõ vào ô text tìm kiếm
        //if (strKey != null)
        //{

        //}
        //else // Trường hợp chọn ở khung biễu mẫu để tìm kiếm
        //{
        if (Request.Params["NoiDi"] != null && Request.Params["NoiDen"] != null && Request.Params["Gia"] != null && Request.Params["NgayDi"] != null)
        {
            string NoiDi = Request.Params["NoiDi"];
            string NoiDen = Request.Params["NoiDen"];
            string GiaTour = Request.Params["Gia"];
            string strLoaiTour = Request.Params["LoaiTour"]; // 1: Tour trong nuoc, 0: Tour nuoc ngoai (Tour Quoc Te)
            int idLoaiTour = -1;
            string whereClauseLoaiTour = "";
            if (strLoaiTour == "1")
            {
                // Get IdLoaiTour Trong Nuoc
                whereClauseLoaiTour = " TenLoaiTour like N'%" + Common.TourTrongNuoc + "%'";
                TList<LoaiTour> tlistLoaiTours = DataRepository.LoaiTourProvider.GetPaged(whereClauseLoaiTour, "ID DESC", 0, 0,
                    out TotalRows);
                if (tlistLoaiTours.Count > 0)
                {
                    idLoaiTour = tlistLoaiTours[0].Id;
                }
            }
            else
            {
                // Get IdLoaiTour Nuoc Ngoai
                whereClauseLoaiTour = " TenLoaiTour like N'%" + Common.TourNuocNgoai + "%'";
                TList<LoaiTour> tlistLoaiTours = DataRepository.LoaiTourProvider.GetPaged(whereClauseLoaiTour, "ID DESC", 0, 0,
                    out TotalRows);
                if (tlistLoaiTours.Count > 0)
                {
                    idLoaiTour = tlistLoaiTours[0].Id;
                }
            }

            //(Ghi chú:Phải định dạng ngày tháng kiểu (dd-MM-yyyy) trong tham số @NgayDi khi truyền) 
            string Date = Request.Params["NgayDi"];
            string[] strArr = Date.Split('-', '/');
            whereClause += " IdSubTour in (Select Id from SubTour where IdLoaiTour = " + idLoaiTour + ") ";
            whereClause += " AND IdDiemDen = " + NoiDen + " AND IdDiemDi = " + NoiDi +
                            " AND ((GiaTour <= " + (GiaTour == "1" ? "1000000" : GiaTour == "2" ? "2000000" : GiaTour == "3" ? "4000000" : GiaTour == "4" ? "6000000" : GiaTour == "5" ? "10000000" : "100000000");
            whereClause += " AND GiaTour > " +
                           (GiaTour == "1"
                               ? "0"
                               : GiaTour == "2"
                                   ? "1000000"
                                   : GiaTour == "3"
                                       ? "2000000"
                                       : GiaTour == "4" ? "4000000" : GiaTour == "5" ? "6000000" : "10000000") + ") ";

            if (strArr[0] == "0" || strArr[1] == "0" || strArr[2] == "0")
            {
                whereClause += "OR (DAY(NgayDi) = " + strArr[0] +
                               " AND MONTH(NgayDi) = " + strArr[1] + " AND YEAR(NgayDi) = " + strArr[2] + "))";
            }
            else
            {
                whereClause += "AND (DAY(NgayDi) = " + strArr[0] +
                           " AND MONTH(NgayDi) = " + strArr[1] + " AND YEAR(NgayDi) = " + strArr[2] + "))";
            }                        
        }

        //}

        if (Request.Params["IdSubTour"] != null)
        {
            whereClause += " IdSubTour= " + Request.Params["IdSubTour"];
        }

        if (Request.Params["TuKhoa"] != null)
        {
            whereClause += " dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + Request.Params["TuKhoa"].ToString() + "%' Or dbo.fuChuyenCoDauThanhKhongDau(TomTat) like N'%" + Request.Params["TuKhoa"].ToString() + "%'";
        }

        TList<Tour> TList_Tour = DataRepository.TourProvider.GetPaged(whereClause, "NgayDi Desc", PageNo - 1, Pager1.PageSize, out TotalRows);
        if (TList_Tour.Count > 0)
        {
            grdGridView.DataSource = TList_Tour;
            grdGridView.DataBind();
            grdGridView.Visible = true;
            ltrMessage.Visible = false;
            // Thay doi mau row
            for (int i = 0; i < grdGridView.Rows.Count; i++)
            {
                if (i % 2 == 0) grdGridView.Rows[i].BackColor = Color.Pink;
            }

            //Hien thi phan trang
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize) Pager1.Visible = false;
            else Pager1.Visible = true;
        }
        else
        {
            ltrMessage.Text = "Thông tin đang cập nhật";
            ltrMessage.Visible = true;
            grdGridView.Visible = false;
        }
        //Hien thi phan trang
        //Pager1.Visible = false;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        DisplayTour(currnetPageIndex);
    }

    protected void Pager2_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager2.CurrentIndex = currnetPageIndex;
        HienThiChuyenMuc(currnetPageIndex);
    }

    public string GetDiemDi(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        NoiDi ent_NoiDi = DataRepository.NoiDiProvider.GetById(s);
        return ent_NoiDi.TenNoiDi;
    }
    public string GetDiemDen(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        NoiDen ent_NoiDen = DataRepository.NoiDenProvider.GetById(s);
        return ent_NoiDen.TenNoiDen;
    }

    public string GetSubTour(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        SubTour ent_SubTour = DataRepository.SubTourProvider.GetById(s);
        return ent_SubTour.TenSubTour;
    }
    public string GetGiaTien(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        GiaTour ent_GiaTour = DataRepository.GiaTourProvider.GetById(s);
        return ent_GiaTour.SoTien;
    }
    public double TruNgay(string str1, string str2)
    {
        DateTime dt1 = Convert.ToDateTime(str1);
        DateTime dt2 = Convert.ToDateTime(str2);
        TimeSpan tspan = dt1.Subtract(dt2);
        return tspan.TotalDays;
    }
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
    protected void grdGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmdDatTour")
        {
            string strId = e.CommandArgument.ToString();
            Tour entTour = DataRepository.TourProvider.GetById(Convert.ToInt32(strId));
            if (entTour.SoCho == 0)
            {
                WebMessage.Show("Số chỗ đã hết! Vui lòng đặt Tour khác!");
            }
            else
            {
                Response.Redirect("~/" + "DatTour/" + entTour.Id + ".aspx");
            }
        }
    }
}